package com.rbac.admin.controller.system;

import com.rbac.admin.annotation.OperationLog;
import com.rbac.admin.common.result.Result;
import com.rbac.admin.service.system.RoleMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 角色菜单管理控制器
 *
 * @author rbac-admin
 * @since 2024-07-29
 */
@RestController
@RequestMapping("/system/roles")
@Tag(name = "角色菜单管理", description = "角色菜单关联相关接口")
public class RoleMenuController {
    
    @Autowired
    private RoleMenuService roleMenuService;
    
    /**
     * 分配角色菜单
     */
    @Operation(summary = "分配角色菜单")
    @OperationLog(module = "权限管理", type = "修改", value = "分配角色菜单权限", recordParams = true)
    @PostMapping("/{roleId}/menus")
    public Result<Boolean> assignRoleMenus(
            @Parameter(description = "角色ID") @PathVariable String roleId,
            @RequestBody Map<String, List<String>> params) {
        try {
            List<String> menuIds = params.get("menuIds");
            if (menuIds == null) {
                return Result.failed("菜单ID列表不能为空");
            }
            boolean success = roleMenuService.assignRoleMenus(roleId, menuIds);
            if (success) {
                return Result.success("分配成功", true);
            } else {
                return Result.failed("分配失败");
            }
        } catch (Exception e) {
            return Result.failed("分配角色菜单失败：" + e.getMessage());
        }
    }
} 